package com.schoolvisitors.controller.admin;

import com.alibaba.fastjson2.JSONObject;
import com.schoolvisitors.CheckPermission;
import com.schoolvisitors.entity.InstrumentSubType;
import com.schoolvisitors.enums.PermissionEnum;
import com.schoolvisitors.response.ResultBody;
import com.schoolvisitors.service.InstrumentService;
import com.schoolvisitors.service.InstrumentSubTypeService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/admin")
public class InstrumentSubTypeController {
    private final InstrumentSubTypeService instrumentSubTypeService;

    public InstrumentSubTypeController(InstrumentSubTypeService instrumentSubTypeService) {
        this.instrumentSubTypeService = instrumentSubTypeService;
    }

    @CheckPermission(PermissionEnum.SYSTEM_ADMIN)
    @GetMapping("/getInstrumentSubTypes")
    public ResultBody getInstrumentSubTypes() {
        List<InstrumentSubType> instruments = instrumentSubTypeService.getAll();
        return ResultBody.success(instruments);
    }

    @CheckPermission(PermissionEnum.SYSTEM_ADMIN)
    @PostMapping("createInstrumentSubType")
    public ResultBody create(@RequestBody InstrumentSubType subType) {
        instrumentSubTypeService.create(subType);
        return ResultBody.success();
    }

    @CheckPermission(PermissionEnum.SYSTEM_ADMIN)
    @PostMapping("/deleteInstrumentSubType")
    public ResultBody deleteInstrumentSubType(@RequestBody JSONObject json) {
        Long id = json.getLong("id");
        instrumentSubTypeService.deleteById(id);
        return ResultBody.success();
    }
}